/*
 * Copyright 2010-2020 Alfresco Software, Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.activiti.engine.impl.el;

import java.util.HashMap;
import java.util.Map;

import javax.el.ELContext;
import javax.el.ELResolver;
import javax.el.FunctionMapper;
import javax.el.VariableMapper;

import com.ziroom.common.api.constants.Constant;

/**
 */
public class ActivitiElContext extends ELContext {

	protected ELResolver elResolver;
	private Map<String, String> contextMap = new HashMap<>(2);

	public ActivitiElContext(ELResolver elResolver) {
		this.elResolver = elResolver;
	}
	
	public ActivitiElContext(ELResolver elResolver, String activityId, String processDefinitionKey) {
		this.elResolver = elResolver;
		contextMap.put(Constant.ACTIVITY_ID, activityId);
		contextMap.put(Constant.PROCESS_DEFINITION_KEY, processDefinitionKey);
	}

	public ELResolver getELResolver() {
		return elResolver;
	}

	public FunctionMapper getFunctionMapper() {
		return new ActivitiFunctionMapper();
	}

	public VariableMapper getVariableMapper() {
		return null;
	}
	
	public String getWorkflowId(){
		return contextMap.getOrDefault(Constant.PROCESS_DEFINITION_KEY, Constant.EMPTY_STRING);
	}
	
	public String getServiceTaskId(){
		return contextMap.getOrDefault(Constant.ACTIVITY_ID, Constant.EMPTY_STRING);
	}
	
	public void setWorkflowId(String processDefinitionKey) {
		contextMap.put(Constant.PROCESS_DEFINITION_KEY, processDefinitionKey);
	}

	public void setServiceTaskId(String activityId) {
		contextMap.put(Constant.ACTIVITY_ID, activityId);
	}
	
	public void setBussinessSpendTime(long spend) {
		contextMap.put(Constant.BUSSINESS_SPEND_TIME, String.valueOf(spend));
	}
	
	public long getBussinessSpendTime() {
		String val = contextMap.get(Constant.BUSSINESS_SPEND_TIME);
		if(val == null) {
			return 0l;
		}
		return Long.parseLong(val);
	}

	public void removeBussinessSpendTime() {
		contextMap.remove(Constant.BUSSINESS_SPEND_TIME);
	}
	
}
